<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文件上传</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<input type="file" id="file">
<button onclick="upload()">上传</button>
缩略图：
<img src="#" alt="">
</body>
  <script>
    /*
    将图片变成base64
    1,获取蹄片
    2.FileReader对象进行转化
      */
    function upload () {
      let file = document.getElementById('file').files[0]
      //创建一个filereader对象
      let fileReader = new FileReader()
      fileReader.onload = function(){
        console.log('图片转化完毕')
        console.log(fileReader.result)
      }
      //用filereader对象转换图片
      fileReader.readAsDataURL(file)
    }

  </script>
</html>

